#include <cstdio>
#include <cstring>

int f[ 7 ] = { 0, 1, 7, 19, 37, 61, 91 };
long long mod;

struct Matrix
{
    long long a[ 91 ][ 91 ];
    int n, m;
};

void operator *= ( Matrix &a, const Matrix &b )
{
    Matrix c;
    int i, j, k;
    c.n = a.n; c.m = b.m;
    for ( i = 0; i < c.n; i++ )
        for ( j = 0; j < c.m; j++ )
            for ( c.a[ i ][ j ] = k = 0; k < a.m; k++ )
                c.a[ i ][ j ] = ( c.a[ i ][ j ] + a.a[ i ][ k ] * b.a[ k ][ j ] ) % mod;
    a = c;
}

int n, l;
Matrix a, b, c;

void init( )
{
    int i, j, cnt = 0;
    memset( a.a, 0, sizeof( a.a ) );
    memset( b.a, 0, sizeof( b.a ) );
    memset( c.a, 0, sizeof( c.a ) );
    a.n = 1; a.m = f[ n ];
    b.n = b.m = c.n = c.m = f[ n ];
    for ( i = 0; i < f[ n ]; i++ )
        b.a[ i ][ i ] = c.a[ i ][ i ] = 1;
    for ( i = 0; i < n; i++ )
        for ( j = 0; j < n + i; j++ )
        {
            if ( i != n - 1 )
            {
                c.a[ cnt + n + i ][ cnt ] = c.a[ cnt + n + i + 1 ][ cnt ] = 1;
                c.a[ cnt ][ cnt + n + i ] = c.a[ cnt ][ cnt + n + i + 1 ] = 1;
            }
            else
            {
                if ( j < n + i - 1 )
                    c.a[ cnt + n + i ][ cnt ] = c.a[ cnt ][ cnt + n + i ] = 1;
                if ( j )
                    c.a[ cnt + n + i - 1 ][ cnt ] = c.a[ cnt ][ cnt + n + i - 1 ] = 1;
            }
            if ( j ) c.a[ cnt - 1 ][ cnt ] = 1;
            if ( j < n + i - 1 ) c.a[ cnt + 1 ][ cnt ] = 1;
            scanf("%I64d", &a.a[ 0 ][ cnt++ ]);
        }
    for ( i = n - 2; i >= 0; i-- )
        for ( j = 0; j < n + i; j++ )
        {
            if ( j )
            {
                if ( i )
                    c.a[ cnt + n + i - 1 ][ cnt ] = c.a[ cnt ][ cnt + n + i - 1 ] = 1;
                c.a[ cnt - 1 ][ cnt ] = 1;
            }
            if ( j < n + i - 1 )
            {
                if ( i )
                    c.a[ cnt + n + i ][ cnt ] = c.a[ cnt ][ cnt + n + i ] = 1;
                c.a[ cnt + 1 ][ cnt ] = 1;
            }
            scanf("%I64d", &a.a[ 0 ][ cnt++ ]);
        }
}

void work( )
{
    int i, j, p = 1, cnt = 0;
    long long ans = 0;
    if ( n == 1 )
    {
        printf("%I64d\n", a.a[ 0 ][ 0 ] % mod);
        return ;
    }
    while ( p <= l ) p <<= 1;
    p >>= 1;
    while ( p )
    {
        b *= b;
        if ( p & l )
            b *= c;
        p >>= 1;
    }
    a *= b;
    for ( i = 0; i < f[ n ]; i++ )
        ans += a.a[ 0 ][ i ] % mod;
    printf("%I64d\n", ans);
}

int main( )
{
    int cas = 1;
    while ( scanf("%d%I64d%d", &n, &mod, &l) != EOF )
    {
        if ( !n && !mod && !l ) break;
        init( );
        printf("Case %d: ", cas++);
        work( );
    }
    return 0;
}
